/*
 * @file    :code.c
 * @module  :CODE
 * @author  :LingFengLu
 * @date    :2023-06-06
 */

#include "code.h"
#include "drv.h"
//Constant definition    宏定义


#define EN_VMO_PIN           GPIO_CHANNEL(PORT_B,PIN0)
#define ADC_EN_VMO_PIN       GPIO_CHANNEL(PORT_B,PIN3)
#define ADC_TEMP_PIN         GPIO_CHANNEL(PORT_B,PIN1)

#define REF_R_VALUE          (10000)
#define R_MAX  50000
//Global variable definition    全局变量定义


//Private variable definition    私有变量定义
static uint32 Code_GetR(void);

//Private function declaration   私有函数申明
static const uint16 m_u16_RList[43]=
{
    511,634,750,845,1020,1240,1430,1650,1910,2100,2320,
    2670,2800,3010,3160,3320,3480,3740,3920,4120,4320,
    4750,4990,5230,5490,5760,6040,6340,6650,6980,7320,7680,
    8450,8870,9310,9760,10200,10700,11300,11800,12700,13300,14000
};

static const sint16 m_s16_Offset[4]=
{
    0,50,100,-50
};  
static const uint8 m_u8_Index[4]=
{
    10,20,31,42
};
static const uint16 m_u16_CF[11]=
{
    60,62,64,66,68,70,72,74,76,78,80
};



//Public function definition   公共函数定义
void Code_Init(void)
{

}

uint Code_SensorParamUpdate
(
    uint16 * u16p_CF,
    sint16 * s16p_Offset
)
{
    sint16 s16_Offset;
    uint16 u16_CF;
    uint i;
    uint32 u32_RValue = Code_GetR();
    if(u32_RValue > R_MAX)
        return FUNCTION_FAIL;
    
    if(u32_RValue <= (m_u16_RList[m_u8_Index[0]] + m_u16_RList[m_u8_Index[0] + 1 ]) / 2)
    {
        s16_Offset  = m_s16_Offset[0];
        for(i = 0 ;i < m_u8_Index[0]; i++)
        {
            if(u32_RValue >= m_u16_RList[i] && u32_RValue<= m_u16_RList[i+1])
            break;
        }
        if(i >= m_u8_Index[0])
            return FUNCTION_FAIL;

        if(u32_RValue <= (m_u16_RList[i]+ m_u16_RList[i+1]) / 2)
            i = i % 11;
        else
            i = (i + 1) % 11;
   
        u16_CF = m_u16_CF[i];
    }
    else if(u32_RValue <= (m_u16_RList[m_u8_Index[1]] + m_u16_RList[m_u8_Index[1] + 1]) / 2 )
    {
         s16_Offset  = m_s16_Offset[1];
        for(i = m_u8_Index[0] + 1 ;i < m_u8_Index[1]; i++)
        {
            if(u32_RValue >= m_u16_RList[i] && u32_RValue <= m_u16_RList[i+1])
            break;
        }
        if( i>= m_u8_Index[1])
            return FUNCTION_FAIL;

        if(u32_RValue <= (m_u16_RList[i]+ m_u16_RList[i+1])/2)
            i = i % 11;
        else
            i = (i + 1) % 11;
            
        if(i >= 5)
            i++;
        u16_CF =m_u16_CF[i];
    }
    else if(u32_RValue <= (m_u16_RList[m_u8_Index[2]] + m_u16_RList[m_u8_Index[2] + 1]) / 2 )
    {
         s16_Offset  = m_s16_Offset[2];

        for(i = m_u8_Index[1] + 1 ;i < m_u8_Index[2]; i++)
        {
            if(u32_RValue>= m_u16_RList[i] && u32_RValue<= m_u16_RList[i+1])
            break;
        }
        if( i >= m_u8_Index[2])
            return FUNCTION_FAIL;

        if(u32_RValue <= (m_u16_RList[i]+ m_u16_RList[i+1]) / 2)
            i = i % 11;
        else
            i = (i + 1) % 11;
            
        i = (i + 1) % 11;
        u16_CF =m_u16_CF[i];

    }
    else 
    {
        s16_Offset  = m_s16_Offset[3];

        for(i = m_u8_Index[2] + 1 ;i < m_u8_Index[3]; i++)
        {
            if(u32_RValue>= m_u16_RList[i] && u32_RValue <= m_u16_RList[i+1])
            break;
        }
        if(i >= m_u8_Index[3])
        {
            i = m_u8_Index[3];
        }
        else
        {

            if(u32_RValue <= (m_u16_RList[i]+ m_u16_RList[i+1]) / 2)
                i = i % 11;
            else
                i = (i + 1) % 11;
                

        }

        i = (i + 1) % 11;
        u16_CF = m_u16_CF[i];

    } 
    *u16p_CF = u16_CF;
    *s16p_Offset = s16_Offset;
    return FUNCTION_OK;
}







//Private function definition   私有函数定义

static uint32 Code_GetR(void)
{
    uint32 u32_R = 0;
    uint32 u32_VddValue = 0;
    uint32 u32_VadValue = 0;
    DrvGpio_Set(EN_VMO_PIN ,MODE_OUTPUT_NO ,1) ;
    Drv_DelayMs(10);
    u32_VddValue = DrvAdc_Single(ADC_EN_VMO_PIN);
    u32_VadValue = DrvAdc_Single(CHANNEL_TEMP);
    DrvGpio_Set(EN_VMO_PIN,MODE_INPUT_DOWN,0);
    if(u32_VddValue > u32_VadValue)
        u32_R = REF_R_VALUE*u32_VadValue/(u32_VddValue-u32_VadValue);
    else
        u32_R = 0;
    return u32_R;
}
